package com.keya.features.job;

import com.keya.config.ClearDataProperties;
import com.keya.config.DiskConfigProperties;
import com.keya.config.JobConfig;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.scheduling.support.CronTrigger;

import javax.annotation.Resource;
import java.io.File;

@Configuration
@EnableScheduling
@Slf4j
@Data
public class MonitorDiskJob implements SchedulingConfigurer {

    public boolean diskIsFull = false;

    @Resource
    JobConfig jobConfig;

    @Resource
    ClearDataProperties clearDataProperties;

    @Resource
    ClearDataJob clearDataJob;

    @Resource
    DiskConfigProperties diskConfigProperties;

    @Override
    public void configureTasks(ScheduledTaskRegistrar scheduledTaskRegistrar) {
        scheduledTaskRegistrar.addTriggerTask(
                () -> {
                    try {
                        job();
                    } catch (Exception e) {
                        log.error("监测硬盘异常", e);
                    }
                },
                triggerContext -> {
                    String cron = jobConfig.getMonitorDisk();
                    return new CronTrigger(cron).nextExecutionTime(triggerContext);
                }
        );
    }

    public void job() {
        diskIsFull = monitorDisk();
        if (diskIsFull) {
            clearDataJob.job();
        }
    }

    public boolean monitorDisk() {
        String surplusCapacityForReject = clearDataProperties.getSurplusCapacityForReject();
        String hdd = diskConfigProperties.getHddPath();
        File hddFile = new File(hdd);
        if (!hddFile.exists()) {
            log.error("hdd路径不存在");
            return false;
        }
        if (surplusCapacityForReject.contains("DISK")) {
            float percent = Float.parseFloat(surplusCapacityForReject.replace("DISK", ""));
            float percent2 = (float) (hddFile.getUsableSpace() * 1.0 / hddFile.getTotalSpace()) * 100;
            if (percent2 < percent) {
                log.error("当前硬盘剩余容量已经严重不足！！！！！" + "hdd剩余" + percent2 + "%！！！！！");
                return true;
            }
            return false;
        } else {
            float surplus = 0;
            float surplushdd = 0;
            String unit = "";
            if (surplusCapacityForReject.contains("GB")) {
                unit = "GB";
                surplus = Float.parseFloat(surplusCapacityForReject.replace("GB", ""));
                surplushdd = hddFile.getUsableSpace() / 1024f / 1024f / 1024f;

            } else if (surplusCapacityForReject.contains("MB")) {
                unit = "MB";
                surplus = Float.parseFloat(surplusCapacityForReject.replace("MB", ""));

                surplushdd = hddFile.getUsableSpace() / 1024f / 1024f;
            }
            if (surplus == 0) {
                log.error("硬盘拒绝策略配置错误");
                return false;
            }
            if (surplus > surplushdd) {
                log.error("当前硬盘剩余容量已经严重不足！！！！！" + "hdd剩余" + surplushdd + unit + "！！！！！");
                return true;
            }
            return false;
        }
    }


}
